// by Xeno
#define THIS_FILE "fn_sideflag.sqf"
#include "x_setup.sqf"
private ["_posi_array", "_ran", "_ran_pos", "_flagtype", "_ini_str", "_flag", "_owner", "_ownerthere"];
if !(call FUNC(checkSHC)) exitWith {};

PARAMS_1(_posi_array);

_ran = _posi_array call FUNC(RandomFloorArray);
_ran_pos = _posi_array select _ran;

_posi_array = nil;

if (GVAR(with_ranked)) then {GVAR(sm_p_pos) = nil};

_flag = createVehicle ["FlagPole_F", _ran_pos, [], 0, "NONE"];
_flag setPos _ran_pos;

_flag setFlagTexture (call FUNC(getenemyflagtex));

switch (GVAR(enemy_side)) do {
	case "BLUFOR": {_flag setflagside blufor};
	case "OPFOR": {_flag setflagside opfor};
	case "INDEPENDENT": {_flag setflagside independent};
};

sleep 2.123;
["shilka", 1, "bmp", 1, "tank", 1, _ran_pos,1,250,true] spawn FUNC(CreateArmor);
sleep 2.123;
["specops", 2, "basic", 4, _ran_pos,150,true] spawn FUNC(CreateInf);

_ran_pos = nil;
_ran = nil;
_flagtype = nil;
_ini_str = nil;

sleep 15.111;

GVAR(sm_flag_failed) = false;
_ownerthere = false;

while {true} do {
	call FUNC(mpcheck);
	_owner = flagOwner _flag;
	
	if (!isNull _owner && {isNil {GV(_owner,GVAR(flagowner))}}) then {
		_ownerthere = true;
		_owner setVariable [QGVAR(flagowner), _owner addMPEventHandler ["MPKilled", {GVAR(sm_flag_failed) = true}]];
	};
	
	#ifndef __TT__
	if (!isNull _owner && {(_owner distance GVAR(FLAG_BASE) < 20)}) exitWith {
		if (GVAR(with_ranked)) then {[QGVAR(sm_p_pos), position GVAR(FLAG_BASE)] call FUNC(NetCallEventToClients)};
		_flag setFlagOwner objNull;
		deleteVehicle _flag;
		GVAR(sm_winner) = 2;
		GVAR(sm_resolved) = true;
		if (GVAR(IS_HC_CLIENT)) then {
			[QGVAR(sm_var), GVAR(sm_winner)] call FUNC(NetCallEventCTS);
		};
	};
	#else
	if (!isNull _owner && {(_owner distance GVAR(EFLAG_BASE) < 20)}) exitWith {
		if (GVAR(with_ranked)) then {[QGVAR(sm_p_pos), position GVAR(EFLAG_BASE)] call FUNC(NetCallEventToClients)};
		_flag setFlagOwner objNull;
		deleteVehicle _flag;
		GVAR(sm_winner) = 1;
		GVAR(sm_resolved) = true;
		if (GVAR(IS_HC_CLIENT)) then {
			[QGVAR(sm_var), GVAR(sm_winner)] call FUNC(NetCallEventCTS);
		};
	};
	if (!isNull _owner && {(_owner distance GVAR(WFLAG_BASE) < 20)}) exitWith {
		if (GVAR(with_ranked)) then {[QGVAR(sm_p_pos), position GVAR(WFLAG_BASE)] call FUNC(NetCallEventToClients)};
		_flag setFlagOwner objNull;
		deleteVehicle _flag;
		GVAR(sm_winner) = 2;
		GVAR(sm_resolved) = true;
		if (GVAR(IS_HC_CLIENT)) then {
			[QGVAR(sm_var), GVAR(sm_winner)] call FUNC(NetCallEventCTS);
		};
	};
	#endif
	
	if (GVAR(sm_flag_failed)  || {_ownerthere && {isNil "_owner" || {isNull _owner}}}) exitWith {
		_flag setFlagOwner objNull;
		deleteVehicle _flag;
		GVAR(sm_winner) = -900;
		GVAR(sm_resolved) = true;
		if (GVAR(IS_HC_CLIENT)) then {
			[QGVAR(sm_var), GVAR(sm_winner)] call FUNC(NetCallEventCTS);
		};
		if (!isNil "_owner" && {!isNull _owner}) then {
			_owner removeMPEventHandler ["MPKilled", GV(_owner,GVAR(flagowner))];
			_owner setVariable [QGVAR(flagowner), nil];
		};
	};
	sleep 5.123;
};

GVAR(sm_flag_failed) = nil;